require "TaskCategory"

QNW_TS_DailyTask = QNW_TS_TaskCategory:derive("QNW_TS_DailyTask")

local this = QNW_TS_DailyTask
this.name = getText("IGUI_QNW_TaskSystem_TaskCategory_Day")

function QNW_TS_DailyTask:new()
    local o = QNW_TS_DailyTask:new()
    setmetatable(o, self)
    self.__index = self
    return o
end

function QNW_TS_DailyTask.registerServer()
    Events.EveryHours.Add(this.everyHoursCallback)
end

function QNW_TS_DailyTask.unregisterServer()
    Events.EveryHours.Remove(this.everyHoursCallback)
end

function QNW_TS_DailyTask.everyHoursCallback()
    if getGameTime():getCalender():get(QNW_TS_Config.Calender.HOUR_OF_DAY) == 1 then
        QNW_TS_ServerCore.generateTask(this, true)
    end
end

function QNW_TS_DailyTask.generateTask(_pool)
    return _pool:randomWeight(20)
end

function QNW_TS_DailyTask.online(_pool, _player, _time)
    if _time then
        if _time + 24 * 60 * 60 * 1000 > getGameTime():getCalender():getTimeInMillis() then
            return
        end
    end
    local taskList = QNW_TS_DailyTask.generateTask(_pool)
    for k, v in pairs(taskList) do
        QNW_TS_ServerCommand.releaseTask(v, _player)
    end
end
